Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
ensure-array
Advanced tools
Ensure that an object is an array. Moves error checking out of your code.
Simple convenience function which ensures that you are dealing with an array and you can eliminate noise from your code.
For Example:
var array = require('ensure-array');
function foo(bar) {
array(bar).forEach(function (x) {
//do something with each item
});
}
Instead of doing something like this:
function foo(bar) {
if (bar === undefined) return;
if (bar === null) return;
if (!Array.isArray(bar)) bar = [bar];
bar.forEach(function (x) {
//do something with each item
});
}
It gets rid of the noise and coerces what is provided into an array, so you do not have to litter your code with a bunch of extraneous checks.
Here is the logic behind the function:
Here is the actual code which makes it happen
module.exports = function array(a, b, n) {
if (arguments.length === 0) return []; //no args, ret []
if (arguments.length === 1) { //single argument
if (a === undefined || a === null) return []; // undefined or null, ret []
if (Array.isArray(a)) return a; // isArray, return it
}
return Array.prototype.slice.call(arguments); //return array with copy of all arguments
};
npm install ensure-array
var array = require('ensure-array'); // get handle to the function
var foo = array(whatever); // foo will now safely be an array
FAQs
Ensure that an object is an array. Moves error checking out of your code.
We found that ensure-array demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.